package tako.wisers.com.android.apis.parser;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import tako.wisers.com.android.apis.item.StationItem;
import tako.wisers.com.android.apis.parser.xml.item.ButtonItem;
import tako.wisers.com.android.apis.parser.xml.item.Table;
import tako.wisers.com.android.apis.util.Global;

import android.content.Context;

public class StationXMLParser extends XMLParserBase{
	private final static String EXTERNAL_TAG = "external";
	public final static String NODE_TAG = "node";
	public final static String PARAM_TAG = "param";
	public final static String DRAWABLE_TAG = "drawable";
	public final static String HANDLER_TAG = "handler";

	public final static String NODE_ATTRIBUTE_ID = "id";
	public final static String NODE_ATTRIBUTE_VALUE = "value";
	public final static String NODE_ATTRIBUTE_ACTIVITY = "activity";
	private final static String SRC_ATTR = "src";
	private final static String TYPE_ATTR = "type";
	private final static String SUB_TYPE_ATTR = "subtype";
	private final static String SHOP_ATTR = "shop";
	private final static String LOCATION_ATTR = "location";
	
	
	private final static String STATION_XML_FILE_NAME = "/list-conf.xml";
	public static final int hight = 0x6f010000;

	private static final StationXMLParser msxp = new StationXMLParser();

	private Map<String, ButtonItem> stationMap = new Hashtable<String, ButtonItem>();

	private StationXMLParser() {
		try {
			build(Global.CONF_PATH + STATION_XML_FILE_NAME);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	private StationXMLParser(String path){
		try {
			build(path);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static StationXMLParser getInstance() {
		return msxp;
	}

	private void nodeTag(Node node) {
		String key = getNodeAttribute(node, NODE_ATTRIBUTE_ID);
		String typeStr = getNodeAttribute(node, TYPE_ATTR);
		String subtypeStr = getNodeAttribute(node, SUB_TYPE_ATTR);
		String shopStr = getNodeAttribute(node, SHOP_ATTR);
		String locationStr = getNodeAttribute(node, LOCATION_ATTR);
		int type = String2Int(typeStr);
		int subtype = String2Int(subtypeStr);
		int shop = String2Int(shopStr);
		int location = String2Int(locationStr);
		stationMap.put(key, new ButtonItem(hight + stationMap.size(), key, type, subtype, shop, location));
	}
	
	private int String2Int(String str) {
		if(str != null) {
			return  new Integer(str);
		}
		return 0;
	}

	protected void tagNameSwitch(Node node) {
		String name = node.getNodeName();
		if (NODE_TAG.equalsIgnoreCase(name))
			nodeTag(node);
		else if (EXTERNAL_TAG.equalsIgnoreCase(name))
			extenalTag(node);
		else if (DRAWABLE_TAG.equalsIgnoreCase(name))
			drawableTag(node);
		else if (HANDLER_TAG.equalsIgnoreCase(name))
			handlerTag(node);
		
		parser(node.getChildNodes());
	}
	
	private void drawableTag(Node node){
		String drawableName = getNodeAttribute(node, NODE_ATTRIBUTE_VALUE);
		Node parentNode = getParentNodeByName(NODE_TAG, node.getParentNode());
		String key = getNodeAttribute(parentNode, NODE_ATTRIBUTE_ID);
		ButtonItem item = stationMap.get(key);
		item.setDrawName(drawableName);
		stationMap.put(key, item);
	}
	
	private void handlerTag(Node node){
		String handlerName = getNodeAttribute(node, NODE_ATTRIBUTE_VALUE);
		Node parentNode = getParentNodeByName(NODE_TAG, node.getParentNode());
		String key = getNodeAttribute(parentNode, NODE_ATTRIBUTE_ID);
		ButtonItem item = stationMap.get(key);
		item.setHandler(handlerName);
		stationMap.put(key, item);
	}
	
	private Node getParentNodeByName(String nodeName, Node parentnode){
		if(parentnode.getNodeName().equalsIgnoreCase(nodeName)){
			return parentnode;
		}else{
			return getParentNodeByName(nodeName, parentnode.getParentNode());
		}
	}

	private void extenalTag(Node node) {
		String src = getNodeAttribute(node, SRC_ATTR);
		StationXMLParser childXML = new StationXMLParser(src);
		Map<String, ButtonItem> tempTable = childXML.getStationMap();
		this.stationMap.putAll(tempTable);
	}
	
	
	
	public Map<String, ButtonItem> getStationMap() {
		return stationMap;
	}
	
	public ButtonItem getButtonById(String key){
		return stationMap.get(key);
	}
	
	

}
